﻿using Common;
using Common.DBOper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace VibrationTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Label now_lab = null;
        public frmMain fod = null;
       public frmChannelSet fod2 = null;
       public  frmRun fod3 = null;
       public frmTest fod4 = null;
        OperateTest opertest = new OperateTest();
        OperateSection operattesection = new OperateSection();
        private void lb_main_Click(object sender, EventArgs e)
        {
            DisplayFrm("主界面");

        }



        public void DisplayFrm(string frmName)
        {
            switch (frmName)
            {
                case "主界面":

                    if (fod4 != null)
                    {

                        fod4.StopTest();
                        fod4.Close();
                        fod4 = null;
                    }
                    panel1.Controls.Clear();

             
                    now_lab = this.lb_main;
                    this.lb_main.ForeColor = Color.Cyan;
                    this.lb_set.ForeColor = Color.White;
                    this.lb_run.ForeColor = Color.White;
                    this.lb_test.ForeColor = Color.White;
                    if (fod == null)
                    {
                        fod = new frmMain();
                    }
                    fod.TopLevel = false;
                    fod.Size = panel1.Size;
                    fod.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
                    panel1.Controls.Add(fod);
                    fod.Show();
                    break;
                case "通道配置":
                    panel1.Controls.Clear();

                    if (fod4 != null)
                    {
                        fod4.StopTest();
                        fod4.Close();
                        fod4 = null;
                    }
                    now_lab = this.lb_set; ;
                    this.lb_main.ForeColor = Color.White;
                    this.lb_set.ForeColor = Color.Cyan;
                    this.lb_run.ForeColor = Color.White;
                    this.lb_test.ForeColor = Color.White;
                    if (fod2 == null)
                    {
                        fod2 = new frmChannelSet();
                    }
                    fod2.TopLevel = false;
                    fod2.Size = panel1.Size;
                    fod2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
                    panel1.Controls.Add(fod2);
                    fod2.Show();
                    break;
                case "在线运行":
                    panel1.Controls.Clear();

                    if (fod4 != null)
                    {
                        fod4.StopTest();
                        fod4.Close();
                        fod4 = null;
                    }
                    now_lab = this.lb_run;
                    this.lb_main.ForeColor = Color.White;
                    this.lb_set.ForeColor = Color.White;
                    this.lb_run.ForeColor = Color.Cyan;
                    this.lb_test.ForeColor = Color.White;
                    if (fod3 == null)
                    {
                        fod3 = new frmRun();
                    }
                    fod3.TopLevel = false;
                    fod3.Size = panel1.Size;
                    fod3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
                    panel1.Controls.Add(fod3);
                    fod3.Show();
                    break;

                case "信号检测":

                    if (Mdl_Class.TestModel == 2)
                    {
                        MessageBox.Show("系统正在进行实验测试,不能进行信号检测！", "提示");
                        return;
                    }
                    panel1.Controls.Clear();
                    now_lab = this.lb_test;
                    this.lb_main.ForeColor = Color.White;
                    this.lb_set.ForeColor = Color.White;
                    this.lb_run.ForeColor = Color.White;
                    this.lb_test.ForeColor = Color.Cyan;
                    if (fod4 == null)
                    {
                        fod4 = new frmTest();
                    }
                    fod4.TopLevel = false;
                    fod4.Size = panel1.Size;
                    fod4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
                    panel1.Controls.Add(fod4);
                    fod4.Show();
                    break;
                default:
                    break;
            }
        }

        private void lb_set_Click(object sender, EventArgs e)
        {
            if (Mdl_Class.NowStectionId > 0)
            {
                foreach (var item in frmMain.frmlinechart.Keys)
                {
                    frmMain.frmlinechart[item].RemoveToolTip();
                }
  
                foreach (var item in frmMain.frmHistorgramchart.Keys)
                {
                    frmMain.frmHistorgramchart[item].RemoveToolTip();
                }
                DisplayFrm("通道配置");
            }
            else 
            {
                MessageBox.Show("至少添加一个section再进行设置", "提示");
            
            }
        }

        private void lb_other_Click(object sender, EventArgs e)
        {
            if (Mdl_Class.NowStectionId > 0)
            {
                foreach (var item in frmMain.frmlinechart.Keys)
                {
                    frmMain.frmlinechart[item].RemoveToolTip();
                }
                foreach (var item in frmMain.frmHistorgramchart.Keys)
                {
                    frmMain.frmHistorgramchart[item].RemoveToolTip();
                }
                DisplayFrm("在线运行");
            }
            else
            {
                MessageBox.Show("至少添加一个section再运行", "提示");

            }
        }

        private void lb_main_MouseEnter(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            lb.ForeColor = Color.Cyan;
        }

        private void lb_main_MouseLeave(object sender, EventArgs e)
        {
            if (now_lab==null||now_lab.Name != ((Label)sender).Name)
            {
            Label lb = sender as Label;
            lb.ForeColor = Color.White;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Program.form1 = this;
            Mdl_Class.FilePath = "";
            DisplayFrm("主界面");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            OpenProject();
            
        }


        public void OpenProject() 
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = true;//该值确定是否可以选择多个文件
            dialog.Title = "请选择项目文件";
            dialog.Filter = "项目文件(*.mdb)|*.mdb";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;

                //if (testlist == null)
                //{
                //    MessageBox.Show("打开项目文件失败", "提示");
                //}
                //else
                //{

                if (fod == null)
                {
                    DisplayFrm("主界面");
                }
                string prjname = dialog.SafeFileName.Split('.')[0];
                if (Mdl_Class.testTopath.ContainsKey(prjname))
                {
                    Mdl_Class.testTopath[prjname] = file;
                }
                else
                {
                    Mdl_Class.testTopath.Add(prjname, file);
                }
                Mdl_Class.FilePath = file;
                DisplaySection();

                fod.LoadFile(prjname);
                //}

            }
        }
        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripDropDownButton4_Click(object sender, EventArgs e)
        {

        }

        private void toolStripDropDownButton5_Click(object sender, EventArgs e)
        {

        }

        private void 下位机设置ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
                if(Mdl_Class.FilePath=="")
            {
                   MessageBox.Show("请打开或者添加项目在进行设置","提示");
                    return;
            }
            frmSet frmset = new frmSet();
            frmset.ShowDialog();
        }


        public void DisplaySection()
        {
            this.cmb_section.Items.Clear();
            Mdl_Class.sectionNameTosectionId.Clear();
            List<Cls_Section> sectionlist = operattesection.GetSectionList();
            if (sectionlist!=null)
            {
            for (int i = 0; i < sectionlist.Count;i++ )
            {
                this.cmb_section.Items.Add(sectionlist[i].SectionName);

                Mdl_Class.sectionNameTosectionId.Add(sectionlist[i].SectionName, sectionlist[i].SectionId);
            }
            }
            if (this.cmb_section.Items.Count > 0)
            {
                this.cmb_section.SelectedIndex = 0;
            }
            else 
            {
                this.cmb_section.SelectedIndex = -1;
            }
        }

        private void cmb_section_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_section.Text != "")
            {
                if (Mdl_Class.sectionNameTosectionId.ContainsKey(cmb_section.Text))
                {
                    Mdl_Class.NowStectionId = Mdl_Class.sectionNameTosectionId[cmb_section.Text];
                    Mdl_Class.NowStectionName = cmb_section.Text;

                    if (Program.formchannelset!=null)
                    {
                        Program.formchannelset.DisplaySet();
                    }
                    if (Program.formRun!=null) 
                    {
                        Program.formRun.LoadChannel();                 
                    }
                    if (Program.formTest != null)
                    {
                        Program.formTest.LoadChannel();
                    }
                }
                else
                {
                    Mdl_Class.NowStectionId = 0;
                    Mdl_Class.NowStectionName = "";
                }
            }
            else 
            {
                Mdl_Class.NowStectionId = 0;
                Mdl_Class.NowStectionName = "";
            }
        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
      
        }

        private void toolStripTextBox3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DisplayFrm("主界面");
            if(Program.frmmain!=null)
            {
                Program.frmmain.AddProject();
                //Program.frmmain.AddSection();
            }
        }

        private void toolStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "新建(&N)":
                      DisplayFrm("主界面");
                    if(Program.frmmain!=null)
                    {
                      Program.frmmain.AddProject();
                     }
                    break;
                case "打开(&O)":

                    OpenProject();
                    break;
                case "保存(&S)":
                    if (Program.formchannelset != null)
                    {
                        Program.formchannelset.SaveChannelSet();
                    }
                    break;
                default:
                    break;
            }
        }

        private void 新建NToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void 添加新的sectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Mdl_Class.FilePath != "")
            {
                if(Program.frmmain!=null)
                {
                    Program.frmmain.AddSection();
                }
            }
            else 
            {
                MessageBox.Show("请先创建或者打开Project","提示");
            }
        }

        private void 添加ProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Program.frmmain != null)
            {
                Program.frmmain.AddProject();

            }
        }


        public void ClearSection() 
        {
            this.cmb_section.Items.Clear();
            this.cmb_section.SelectedIndex = -1;
        }

        private void 折线图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(Program.frmmain!=null)
            {
                //if (Mdl_Class.FilePath != "")
                //{
                Program.frmmain.AddZX_Chart();
                //}
                //else 
                //{
                    //MessageBox.Show("请先打开或者添加Project","提示");
                //}
            }
        }

        private void 柱状图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Program.frmmain != null)
            {
                //if (Mdl_Class.FilePath != "")
                //{
                    Program.frmmain.AddZZ_Chart();
                //}
                //else
                //{
                //    MessageBox.Show("请先打开或者添加Project", "提示");
                //}
            }
        }

        private void 产品列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProduct frmproduct = new frmProduct();
            frmproduct.ShowDialog();
        }

        public void SetCmb_Section(bool yorn)
        {
            this.cmb_section.Enabled = yorn;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Program.frmmain.ClosePrj();
        }

        private void lb_test_Click(object sender, EventArgs e)
        {
            if (Mdl_Class.NowStectionId > 0)
            {
                foreach (var item in frmMain.frmlinechart.Keys)
                {
                    frmMain.frmlinechart[item].RemoveToolTip();
                }
                foreach (var item in frmMain.frmHistorgramchart.Keys)
                {
                    frmMain.frmHistorgramchart[item].RemoveToolTip();
                }
                DisplayFrm("信号检测");
            }
            else
            {
                MessageBox.Show("至少添加一个section再运行", "提示");

            }
        }
    }
}

